package secondStage.part3.Map集合.HashMap集合遍历;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName: [HashMapTest]
 * @Description: HashMap集合存储学生对象并遍历
 * @Author: [黄劲松]
 * @CreateDate: [2021-04-03]
 * @Version: [v1.0]
 */
/*需求:
   创建一个HashMap集合，键是学号(String)，值是学生对象(Student)。存储三个键值对元素，并遍历
思路:
   1:定义学生类
   2:创建HashMap集合对象3:创建学生对象
   4:把学生添加到集合5:遍历集合
        方式1:键找值
        方式2:键值对对象找键和值*/
public class HashMapTest {
    public static void main(String[] args) {
        //创建HashMap集合对象
        HashMap<String, Student> hm = new HashMap<>();
        //创建学生对象
        Student s1 = new Student("使用", 12);
        Student s2 = new Student("汇款", 34);
        Student s3 = new Student("㕣的", 22);
        //把学生添加到集合
        hm.put("10001", s1);
        hm.put("10002", s2);
        hm.put("10003", s3);
        //方式1:键找值
        Set<String> keySet = hm.keySet();
        for (String key : keySet) {
            Student value = hm.get(key);
            System.out.println(key + "," + value.getName() + "," + value.getAge());
        }
        System.out.println("----------");
        //方式2:键值对对象找键和值
        Set<Map.Entry<String, Student>> entrySet = hm.entrySet();
        for (Map.Entry<String, Student> me : entrySet) {
            String key = me.getKey();
            Student value = me.getValue();
            System.out.println(key + "," + value.getName() + "," + value.getAge());
        }
    }
}
